package com.jiaquyun.jcps.ui.mine

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.SPStaticUtils
import com.jiaquyun.jcps.R
import com.jiaquyun.jcps.app.BaseApplication
import com.jiaquyun.jcps.base.ActivityCollector
import com.jiaquyun.jcps.base.ViewModeActivity
import com.jiaquyun.jcps.base.loadCircleImage
import com.jiaquyun.jcps.mode.vm.MineViewModel
import com.jiaquyun.jcps.popup.ConfirmCancelPopup
import com.jiaquyun.jcps.ui.access.AccessActivity
import com.jiaquyun.jcps.ui.main.ConfigActivity
import com.jiaquyun.jcps.utils.AMapLocationUtils
import com.lemlin.network.HttpServiceStatus
import kotlinx.android.synthetic.main.activity_mine.*

class MineActivity : ViewModeActivity<MineViewModel>() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, MineActivity::class.java)
            activity.startActivity(intent)
        }
    }

    private val confirmCancelPopup: ConfirmCancelPopup by lazy {
        ConfirmCancelPopup(
            context = this,
            onConfirm = { any: Any?, action: ConfirmCancelPopup.Action? ->
                confirmCancelPopup.dismiss()
                if (action == null)
                    return@ConfirmCancelPopup
                if (action == ConfirmCancelPopup.Action.CONFIRM_CALL) {
                    val intent = Intent(Intent.ACTION_DIAL)
                    intent.data = Uri.parse("tel:${any.toString()}")
                    startActivity(intent)
                } else if (action == ConfirmCancelPopup.Action.CONFIRM_EXIT) {
                    SPStaticUtils.clear()
                    AMapLocationUtils.instance()?.stopLocation()
                    AMapLocationUtils.instance()?.onDestroy()
                    ActivityCollector.finishAll()
                    AccessActivity.open(activity = this)
                }
            },
            onCancel = { _: Any?, _: ConfirmCancelPopup.Action? ->
                confirmCancelPopup.dismiss()
            })
    }

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = false, color = R.color.colorMain, view = flMineBar)
    }

    override fun initView() {

        ivMineUserHeads.loadCircleImage(
            src = "http://dmc.icarefz.cn${BaseApplication.userInfo()?.userinfo?.avatar}",
            borderSize = 10,
            borderColor = Color.WHITE
        )

        ivMineBack.setOnClickListener {
            finish()
        }
        //考勤打卡
        tvMineSettleSign.setOnClickListener {
            MineSignActivity.open(activity = this)
        }

        //结算记录
        tvMineSettleRecord.setOnClickListener {
            SettleRecordActivity.open(activity = this)
        }

        //在线客服
        tvMineOnlineService.setOnClickListener {
            viewModel.configValue("kefu_mobile")
        }
        //帮助反馈
        tvMineHelpFeedback.setOnClickListener {
            ConfigActivity.open(
                activity = this,
                title = "帮助反馈",
                id = ConfigActivity.Type.HELP_TEXT.id
            )
        }
        //关于我们
        tvMineAboutUs.setOnClickListener {
            ConfigActivity.open(
                activity = this,
                title = "关于我们",
                id = ConfigActivity.Type.ABOUT_US.id
            )
        }

        //个人信息
        ivMineUserHeads.setOnClickListener {
            UserInfoActivity.open(activity = this)
        }
        //个人信息
        tvMineUsername.setOnClickListener {
            UserInfoActivity.open(activity = this)
        }

        //今日订单
        llMineTodayOrder.setOnClickListener {
            TodayOrderActivity.open(activity = this)
        }

        //今日收入
        llMineTodayIncome.setOnClickListener {
            TodayIncomeActivity.open(activity = this)
        }

        BaseApplication.userInfo()?.let {
            tvMineUsername.text = it.userinfo.nickname
        }

        //用户协议
        tvMineAgreementUser.setOnClickListener {
            ConfigActivity.openUrl(
                activity = this,
                title = "用户协议",
                url = "https://mall.jiaquyun.cn/rider/common/get_article_details?id=10"
            )
        }

        //隐私协议
        tvMineAgreementPrivacy.setOnClickListener {
            ConfigActivity.openUrl(
                activity = this,
                title = "隐私协议",
                url = "https://mall.jiaquyun.cn/rider/common/get_article_details?id=14"
            )
        }

        //退出登录
        tvMineExit.setOnClickListener {
            confirmCancelPopup.setMessage("是否退出登录？")
                .setAction(ConfirmCancelPopup.Action.CONFIRM_EXIT)
                .setAny(null)
                .showPopupWindow()
        }
    }

    override fun initData() {
        viewModel.personalData()
    }

    override fun initViewMode() {
        viewModel.personalLiveData.observe(this, Observer { response ->

            if (response.statusCode == HttpServiceStatus.Status.FAILURE) {
                toast(response.statusTip)
            }
            if (response.statusCode == HttpServiceStatus.Status.SUCCESS) {
                response.httpData?.data?.let {
                    tvMineOrderNum.text = it.num.toString()
                    tvMineOrderPrice.text = it.money.toString()
                }
            }
        })
        viewModel.configValueLiveData.observe(this, Observer { response ->

            if (response.statusCode == HttpServiceStatus.Status.FAILURE) {
                toast(response.statusTip)
            }
            if (response.statusCode == HttpServiceStatus.Status.SUCCESS) {
                response.httpData?.data?.let {
                    confirmCancelPopup.setMessage("联系电话：${it}")
                        .setAction(ConfirmCancelPopup.Action.CONFIRM_CALL)
                        .setAny(it)
                        .showPopupWindow()
                }
            }
        })
    }
}